package ynu.edu.model.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import ynu.edu.common.domain.PageDTO;
import ynu.edu.model.entity.Img2Img;
import ynu.edu.model.mapper.Img2ImgMapper;
import ynu.edu.model.service.Img2ImgService;


import java.util.*;

/**
 * Img2Img服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class Img2ImgServiceImpl extends ServiceImpl<Img2ImgMapper, Img2Img> implements Img2ImgService {


    @Override
    public Img2Img getByTaskId(String taskId) {
        LambdaQueryWrapper<Img2Img> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Img2Img::getTaskId, taskId);
        return getOne(wrapper);
    }

    @Override
    public List<Img2Img> getByStatus(String status) {
        LambdaQueryWrapper<Img2Img> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Img2Img::getStatus, status)
                .orderByDesc(Img2Img::getCreateTime);
        return list(wrapper);
    }

    @Override
    public List<Img2Img> getByModelName(String modelName) {
        LambdaQueryWrapper<Img2Img> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Img2Img::getModelName, modelName)
                .orderByDesc(Img2Img::getCreateTime);
        return list(wrapper);
    }

    @Override
    public PageDTO<Img2Img> getPage(int page, int size) {
        Page<Img2Img> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<Img2Img> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Img2Img::getCreateTime);
        Page<Img2Img> result = page(pageParam, wrapper);
        return PageDTO.of(result, Img2Img.class);
    }

    @Override
    public long countRecords() {
        return count();
    }

    @Override
    public long countSuccessRecords() {
        LambdaQueryWrapper<Img2Img> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Img2Img::getStatus, "success");
        return count(wrapper);
    }

    @Override
    public long countFailedRecords() {
        LambdaQueryWrapper<Img2Img> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Img2Img::getStatus, "failed");
        return count(wrapper);
    }
    
    @Override
    public Map<String, Object> getStats() {
        try {
            Map<String, Object> stats = new HashMap<>();
            stats.put("totalRecords", countRecords());
            stats.put("successRecords", countSuccessRecords());
            stats.put("failedRecords", countFailedRecords());
            
            // 计算成功率
            long total = countRecords();
            if (total > 0) {
                double successRate = (double) countSuccessRecords() / total * 100;
                stats.put("successRate", String.format("%.2f%%", successRate));
            } else {
                stats.put("successRate", "0.00%");
            }
            
            return stats;
        } catch (Exception e) {
            log.error("获取统计信息失败", e);
            throw new RuntimeException("获取统计信息失败: " + e.getMessage(), e);
        }
    }
    
    @Override
    public boolean deleteByTaskId(String taskId) {
        LambdaQueryWrapper<Img2Img> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Img2Img::getTaskId, taskId);
        return remove(wrapper);
    }
} 